//
//  HQMainViewController.m
//  WBOC
//
//  Created by sheji on 16/3/16.
//  Copyright © 2016年 hanjiaqi. All rights reserved.
//

#import "HQMainViewController.h"
#import "HQHomeViewController.h"
#import "HQMessageController.h"
#import "HQDiscoverViewController.h"
#import "HQProfileViewController.h"

@interface HQMainViewController ()
/**
 *  撰写按钮
 */
@property (nonatomic,strong)UIButton *composeButton;
@end

@implementation HQMainViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self addChildViewControllers];
}

#pragma mark - 视图生命周期
-(void)viewWillLayoutSubviews
{
    [super viewWillLayoutSubviews];
    [self setupComposeButton];
}

#pragma mark - 设置UI
-(void)setupComposeButton
{
    CGRect rect = self.tabBar.bounds;
    CGFloat w = rect.size.width / self.childViewControllers.count - 1;
    self.composeButton.frame = CGRectInset(rect, 2*w , 0);
}

/**
 *  添加所有的子控制器
 */
-(void)addChildViewControllers
{
    //设置tabbar的tintcolor
    self.tabBar.tintColor = [UIColor orangeColor];
    
    [self addChildViewControllers:[[HQHomeViewController alloc]init] title:@"首页" imageName:@"tabbar_home"];
    [self addChildViewControllers:[[HQMessageController alloc]init] title:@"消息" imageName:@"tabbar_message_center"];
    //添加一个空白的控制器
    [self addChildViewController:[[UIViewController alloc] init]];

    [self addChildViewControllers:[[HQDiscoverViewController alloc]init] title:@"发现" imageName:@"tabbar_discover"];
    [self addChildViewControllers:[[HQProfileViewController alloc]init] title:@"我的" imageName:@"tabbar_profile"];
}


/**
 *  添加子控制器
 *
 *  @param childController 子控制奇
 *  @param title           标题
 *  @param imageName       图片
 */
-(void)addChildViewControllers:(UIViewController *)childController title: (NSString *)title imageName:(NSString*)imageName
{
    childController.title = title;
    NSString *selectedImageName = [NSString stringWithFormat:@"%@_selected",imageName];
    childController.tabBarItem.image = [[UIImage imageNamed:selectedImageName]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    //添加子控制器
    UINavigationController *nav = [[UINavigationController alloc]initWithRootViewController:childController];
    [self addChildViewController:nav];
}

#pragma mark - 懒加载控件
-(UIButton *)composeButton
{
    if (_composeButton == nil) {
        _composeButton = [[UIButton alloc]init];
        
        //设置按钮图像
        [_composeButton setImage:[UIImage imageNamed:@"tabbar_compose_icon_add"] forState:UIControlStateNormal];
        [_composeButton setImage:[UIImage imageNamed:@"tabbar_compose_icon_add_highlighted"] forState:UIControlStateHighlighted];
        [_composeButton setBackgroundImage:[UIImage imageNamed:@"tabbar_compose_button" ]forState:UIControlStateNormal];
        [_composeButton setBackgroundImage:[UIImage imageNamed:@"tabbar_compose_button_highlighted" ]forState:UIControlStateHighlighted];
        
        //添加到tabbar
        [self.tabBar addSubview:_composeButton];
        
        //添加按钮监听方法
        [_composeButton addTarget:self action:@selector(clickComposeButton) forControlEvents:UIControlEventTouchUpInside];
        
    }
    
    return _composeButton;
}
#pragma mark - 撰写按钮的监听方法
-(void)clickComposeButton
{
    NSLog(@"你点我了");
}
@end

